<template>
	<view class="app">
		<view class="cell">
			<text class="tit fill">头像</text>
			<!-- @click="chooseImage" -->
			<view class="avatar-wrap" >
				<image class="avatar" :src="tempAvatar || player.avatar || '/static/icon/default-avatar.png'" mode="aspectFill"></image>
				<!-- 进度遮盖 -->
				<view 
					class="progress center"
					:class="{
						'no-transtion': uploadProgress === 0,
						show: uploadProgress != 100
					}"
					:style="{
						width: uploadProgress + '%',
						height: uploadProgress + '%',
					}"
				></view>
			</view> 
		</view>
		<view class="cell b-b">
			<text class="tit fill">姓名</text>
			<input class="input" v-model="player.name" type="text" maxlength="20" placeholder="请输入姓名" placeholder-class="placeholder">
		</view>
		<view class="cell b-b">
			<text class="tit fill">电话</text>
			<input class="input" v-model="player.phone" type="text" maxlength="11" placeholder="请输入电话" placeholder-class="placeholder">
		</view>
		
		<mix-button ref="confirmBtn" text="保存资料" marginTop="80rpx" @onConfirm="confirm"></mix-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				uploadProgress: 100, //头像上传进度
				tempAvatar: '', 
				player: {},
			}
		},
		computed: {
			curUserInfo(){
				return this.$store.state.player
			}
		},
		watch: {
			curUserInfo(curUserInfo){
				// const {avatar, name,  phone} = curUserInfo;
				// this.player = {avatar, name, phone};
				// const data = curUserInfo;
				// this.player = data;
			}
		},
		onLoad() {
			// const {avatar, name,  phone} = this.curUserInfo;
			// this.player = {avatar, name, phone};
			const data = this.curUserInfo;
			this.player = data;
			// console.log("==this.player="+JSON.stringify(this.player))
		},
		methods: {
			//提交修改
			async confirm(){
				const {uploadProgress, player, curUserInfo} = this;
				let isUpdate = false;
				// console.log("==this.player="+JSON.stringify(player))
				// console.log("==this.curUserInfo="+JSON.stringify(curUserInfo))
				// for(let key in player){
				// 	if(player[key] !== curUserInfo[key]){
				// 		isUpdate = true;
				// 		break;
				// 	}
				// }
				// if(isUpdate === false){
				// 	this.$util.msg('信息未修改');
				// 	this.$refs.confirmBtn.stop();
				// 	return;
				// }
				// if(!player.avatar){
				// 	this.$util.msg('请上传头像');
				// 	this.$refs.confirmBtn.stop();
				// 	return;
				// }
				// if(uploadProgress !== 100){
				// 	this.$util.msg('请等待头像上传完毕');
				// 	this.$refs.confirmBtn.stop();
				// 	return;
				// }
				if(!player.name){
					this.$util.msg('请输入您的姓名');
					this.$refs.confirmBtn.stop();
					return;
				}
				if(!player.phone){
					this.$util.msg('请输入您的电话');
					this.$refs.confirmBtn.stop();
					return;
				}
				// console.log("==sbmit player="+JSON.stringify(player))
				const res = await this.$post('userinfo', 'PlayerEdit', player);
				// const res = await this.$request('user', 'update', player);
				// console.log("==result res="+JSON.stringify(res))
				this.$refs.confirmBtn.stop();
				this.$util.msg(res.msg);
				if(res.code === 200){
					this.$store.dispatch('getPlayer'); //刷新用户信息
					setTimeout(()=>{
						uni.navigateBack();
					}, 1000)
				}
				// const res = await this.$post('publish', 'GameAdd', data);
				// this.$util.msg("发布成功！");
				// this.$refs.confirmBtn.stop();
				// setTimeout(()=>{
				// 	// uni.navigateBack();
				// 	uni.switchTab({
				// 		url: '/pages/tabbar/home'
				// 	})
				// }, 1000)
			},
			//选择头像
			chooseImage(){
				uni.chooseImage({
					count: 1,
					success: res=> {
						uni.navigateTo({
							url: `./cutImage/cut?src=${res.tempFilePaths[0]}` 
						});
					}
				});
			}, 
			//裁剪回调
			async setAvatar(filePath){
				this.tempAvatar = filePath;
				this.uploadProgress = 0;
				const result = await uniCloud.uploadFile({
					filePath: filePath,
					cloudPath: + new Date() + ('000000' + Math.floor(Math.random() * 999999)).slice(-6) + '.jpg',
					onUploadProgress: e=> {
						this.uploadProgress = Math.round(
							(e.loaded * 100) / e.total
						);
					}
				});
				if(!result.fileID){
					this.$util.msg('头像上传失败');
					return;
				}
				if(typeof uniCloud.getTempFileURL === 'undefined'){
					this.player.avatar = result.fileID;
				}else{
					const tempFiles = await uniCloud.getTempFileURL({
						fileList: [result.fileID]
					})
					const tempFile = tempFiles.fileList[0];
					if(tempFile.code === 'SUCCESS'){
						this.player.avatar = tempFile.download_url;
					}else{
						this.$util.msg('头像上传失败');
					}
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.app{
		padding-top: 16rpx;
	}
	.cell{
		display: flex;
		align-items: center;
		min-height: 110rpx;
		padding: 0 40rpx;
		
		&:first-child{
			margin-bottom: 10rpx;
		}
		&:after{
			left: 40rpx;
			right: 40rpx;
			border-color: #d8d8d8;
		}
		.tit{
			font-size: 30rpx;
			color: #333;
		}
		.avatar-wrap{
			width: 120rpx;
			height: 120rpx;
			position: relative;
			border-radius: 100rpx;
			overflow: hidden;
			
			.avatar{
				width: 100%;
				height: 100%;
				border-radius: 100rpx;
			}
			.progress{
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translate(-50%, -50%);
				width: 100rpx;
				height: 100rpx;
				box-shadow: rgba(0,0,0,.6) 0px 0px 0px 2005px;
				border-radius: 100rpx;
				transition: .5s;
				opacity: 0;
				
				&.no-transtion{
					transition: 0s;
				}
				&.show{
					opacity: 1;
				}
			}
		}
		.input{
			flex: 1;
			text-align: right;
			font-size: 28rpx;
			color: #333;
		}
		switch{
			margin: 0;
			transform: scale(0.8) translateX(10rpx);
			transform-origin: center right;
		}
		.tip{
			margin-left: 20rpx;
			font-size: 28rpx;
			color: #999;
		}
		.checkbox{
			padding: 12rpx 0 12rpx 40rpx;
			font-size: 28rpx;
			color: #333;
			
			.mix-icon{
				margin-right: 12rpx;
				font-size: 36rpx;
				color: #ccc;
			}
			.icon-xuanzhong{
				color: $base-color;
			}
		}
	}
</style>
